package com.bw.zg2.view.activity

import android.os.Bundle
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.zg2.R
import com.bw.zg2.base.BaseActivity
import com.bw.zg2.databinding.ActivityMainBinding
import com.bw.zg2.viewmodel.LoginViewModel
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener


@Route(path = "/main/MainActivity")
class MainActivity : BaseActivity<LoginViewModel, ActivityMainBinding>(R.layout.activity_main) {


//    @Autowired
//    lateinit var name: String
//
//    @Autowired
//    lateinit var pass: String


//    @JvmField
//    @Autowired
//    var age: Int = 0

    lateinit var navHostFragment: NavHostFragment
    private var tabArr = mutableListOf<String>("首页", "分类", "购物车", "我的")
    private var tabResArr = mutableListOf<Int>(
        R.drawable.ic_home_24,
        R.drawable.ic_type_24,
        R.drawable.ic_shopping_cart_24,
        R.drawable.ic_mine_24
    )

    override fun initView(savedInstanceState: Bundle?) {
//        ARouter.getInstance().inject(this)



        //获取navHostFragment，宿主容器
        navHostFragment =
            supportFragmentManager.findFragmentById(R.id.fragment_id) as NavHostFragment
        //获取到宿主容器的navController
        val navController = navHostFragment.navController

        dataBinding.navBottom.setupWithNavController(navController)

//        dataBinding.tabLayout.addTab(dataBinding.tabLayout.newTab().setIcon(R.drawable.ic_phone_24).setText("首页"))
//        dataBinding.tabLayout.addTab(dataBinding.tabLayout.newTab().setIcon(R.drawable.ic_phone_24).setText("分类"))
//        dataBinding.tabLayout.addTab(dataBinding.tabLayout.newTab().setIcon(R.drawable.ic_phone_24).setText("购物车"))
//        dataBinding.tabLayout.addTab(dataBinding.tabLayout.newTab().setIcon(R.drawable.ic_phone_24).setText("我的"))
        //设置tablayout 数据，底部tab 的名称
        tabArr.forEachIndexed { index, _ ->
            dataBinding.tabLayout.addTab(
                dataBinding.tabLayout.newTab().setIcon(tabResArr[index]).setText(tabArr[index])
            )
        }

        //跳转到 CartFragment
        dataBinding.tabLayout.addOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                when (tab.position) {
                    0 -> {
                        navController.navigate(R.id.homeFragment)
                    }

                    1 -> {
                        navController.navigate(R.id.typeFragment)
                    }

                    2 -> {
                        navController.navigate(R.id.cartFragment)
                    }

                    3 -> {
                        navController.navigate(R.id.mineFragment)
                    }
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

        })
    }

    override fun initData() {
        //测试提交
//        DbUtils.shopDB
//        DbUtils.getShopDB()
    }


}